//声明: 该文件需要 swiper,jqueryui,common,toast,jquery

require(["config"], function(config) {
	require(["com", "jquery", "sw","fly"], function(com, jq, sw,fly) {
		//购物车
		$(function() {
			var offset = $("#end").offset();
			$(".addcar").click(function(event) {
				var geshu =parseInt($(".num").val());
				var num = parseInt($("#shoppingnum").html())+geshu;
				setCookie("num",num,10);
				$(this).css("background", "gainsboro");
				var addcar = $(this);
				var img = $("#smallimg").find('img').attr('src');
				var flyer = $('<img id="u-flyer" src="' + img + '">');
				flyer.fly({
					start: {
						left: event.clientX,

						top: event.clientY
					},
					end: {
						left: offset.left,
						top: 340,
						width: 0,
						height: 0
					},
					onEnd: function() {
					
						$("#shoppingnum").html(num);
						$("#msg").show().animate({ width: '250px' }, 200).fadeOut(1000);
						//	addcar.css("cursor", "default").removeClass('orange').unbind('click');
						//this.destory();
					}
				});
			});

		});
		//右边的cookie
		function setCookie(key, val, expires) {
			var now = new Date();
			now.setDate(now.getDate() + expires);
			document.cookie = key + "=" + val + "; expires=" + now;
		}

		function getCookie(key) {
			var cookiestr = document.cookie;
			var list = cookiestr.split("; ");
			for(var attr in list) {
				var kv = list[attr].split("=");
				if(kv[0] == key) {
					return kv[1];
				}
			}
			return "";
		}
		
		//导航
		(function() {
			var $subblock = $(".subpage"),
				$head = $subblock.find('h2'),
				$ul = $("#proinfo"),
				$lis = $ul.find("li"),
				inter = false;

			$(".pros").hover(function() {
				$("#proinfo").show();
			}, function() {
				$("#proinfo").hide();

			})

			$lis.hover(function() {
				if(!$(this).hasClass('nochild')) {
					$(this).addClass("prosahover");
					$(this).find(".prosmore").removeClass('hide');
				}
			}, function() {
				if(!$(this).hasClass('nochild')) {
					if($(this).hasClass("prosahover")) {
						$(this).removeClass("prosahover");
					}
					$(this).find(".prosmore").addClass('hide');
				}
			});

		})();
		//我的信息
		$(document).ready(function() {
			//    nav-li hover e
			var num;
			$("#li-1").hover(function() {
				//console.log("hah")
				/*图标向上旋转*/
			//	$(this).children().removeClass().addClass('hover-up');
				/*下拉框出现*/
				//var Obj = $(this).attr('id');
				//num = Obj.substring(3, Obj.length);
				//$('#box-' + num).slideDown(300);
				$('.hidden-box').show()
			}, function() {
				/*图标向下旋转*/
				//$(this).children().removeClass().addClass('hover-down');
				/*下拉框消失*/
				$(".hidden-box").hide();
				//$('#box-' + num).hide();
			});
			//    hidden-box hover e
			$('.hidden-box').hover(function() {
				/*保持图标向上*/
				//$('#li-' + num).children().removeClass().addClass('hover-up');
				$(this).show();
			}, function() {
				$(this).hide();
			});
		});
		
		//我的收藏
		$(document).ready(function() {
		
		
			$("#li-2").hover(function() {
			
				$('.hidden-box2').show();
			}, function() {
				/*图标向下旋转*/
			
				$('.hidden-box2').hide();
			;
			});
			
			$('.hidden-box2').hover(function() {
			
				$(this).show();
			}, function() {
				$(this).hide();
			});
		});
		//数量加减效果
		$(document).ready(function(){
			//加的效果
			$(".add").click(function(){
			var n=$(this).prev().val();
			var num=parseInt(n)+1;
			if(num==0){ return;}
			$(this).prev().val(num);
			});
			//减的效果
			$(".jian").click(function(){
			var n=$(this).next().val();
			var num=parseInt(n)-1;
			if(num==0){ return}
			$(this).next().val(num);
			});
		})
		
		//放大镜
		$(function(){
			
		
				var smallimg = document.getElementById("smallimg");
				var smallglass = document.getElementById("smallglass");
				var bigglass = document.getElementById("bigglass");
				var bigimg = document.getElementById("bigimg");
				//小放大镜/小图片= 大放大镜/大图片
				var _width = smallimg.offsetWidth * bigglass.offsetWidth/bigimg.offsetWidth;
				var _height = smallimg.offsetHeight * bigglass.offsetHeight/bigimg.offsetHeight;
				//当小放大镜宽可高
				smallglass.style.width = _width +"px";
				smallglass.style.height = _height +"px";
				//设置倍率
				var scalex = bigglass.offsetWidth/_width;
				var scaley = bigglass.offsetHeight/_height;
				
				//鼠标移到小放大镜事件
				smallimg.onmouseover = function(){
					bigglass.style.visibility = "visible";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ;
					smallglass.style.display = "block";
				}
				smallimg.onmouseout = function(){
					bigglass.style.visibility = "hidden"; 
					smallglass.style.display ="none";
				}
				//小放大镜移动事件
				smallimg.onmousemove = function(e){
					var _left = e.clientX - smallimg.offsetLeft-smallglass.offsetWidth/2;
					var _top = e.clientY - smallimg.offsetTop - smallglass.offsetHeight/2+$(window).scrollTop();
					if(_left<=0){//在最左边
						_left=0;
					}
					//最右边
					if(_left >=smallimg.offsetWidth-smallglass.offsetWidth){
						_left= smallimg.offsetWidth-smallglass.offsetWidth;
					}
					if(_top<=0){//最上边
						_top = 0;
					}
					//最下边
					if(_top>=smallimg.offsetHeight-smallglass.offsetHeight){
						_top = smallimg.offsetHeight-smallglass.offsetHeight;
					}
					
					smallglass.style.left = _left +"px";
					smallglass.style.top = _top +"px";
					
					bigimg.style.left = -_left * scalex +"px";
					bigimg.style.top = -_top *scaley +"px";
					
				}
		
			})
		//2d动画
		$("#2danimate li").each(function(){
			$(this).hover(function(){
				$(this).find("p").css("transform","translateY(-50px)");
				
			},function(){
				$(this).find("p").css("transform","translateY(0px)");
				
			})
		})
		//left right go
		$("#leftgo").click(function(){
			$(this).css("color","gainsboro");
			$("#rightgo").css("color","black");
			$("#2danimate").css("transform","translateX(0px)")
		})
		$("#rightgo").click(function(){
			$("#leftgo").css("color","black");
			$(this).css("color","gainsboro");
			$("#2danimate").css("transform","translateX(-200px)");
		})
			
		//轮播图
		
		var mySwiper = new Swiper('.swiper-container', {
			autoplay: 1000,//可选选项，自动滑动
			direction : 'vertical',
			loop : true,
			pagination : '.swiper-pagination',
		})
		//楼梯效果
		var _index=0;
		$("#wrap li").click(function(){
			 $(this).find("span").addClass("active").parent().siblings().find("span").removeClass("active");
			   _index=$(this).index()+1;
			   //通过拼接字符串获取元素，再取得相对于文档的高度
			   var _top=$("#louti"+_index).offset().top;
			   //scrollTop滚动到对应高度
			   $("body,html").animate({scrollTop:_top},500);		  
		});
		$("#wrap1 li").click(function(){
		
			 $(this).find("span").addClass("active").parent().siblings().find("span").removeClass("active");
			   _index=$(this).index()+1;
			   //通过拼接字符串获取元素，再取得相对于文档的高度
			   var _top=$("#louti"+_index).offset().top;
			   //scrollTop滚动到对应高度
			   $("body,html").animate({scrollTop:_top},500);		  
		});
			var win = $(window); //得到窗口对象
			var sc = $(document); //得到document文档对象。
			win.scroll(function() {
				
			if($(document).scrollTop()>1000){
			
				$("#wrap").hide();
				$("#wrap1").show();
			}else{
				$("#wrap").show();
				$("#wrap1").hide();
			}
		
		})
		//详情页
		$("#tupian2").mouseover(function(){
			$("#smallimg img").attr("src","images/2f8.jpg");
			$("#bigimg").attr("src","images/2f8.jpg");
		})
		$("#tupian1").mouseover(function(){
			$("#smallimg img").attr("src","images/2f1.jpg");
			$("#bigimg").attr("src","images/2f1.jpg");
		})
		
	
	})
})